//
//  SEGMenuListCacheManager.m
//  SEGUhomeStandard
//
//  Created by 乔杨 on 2019/5/27.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGMenuListCacheManager.h"

#if __has_include(<Commom/SEGProfileManager.h>)
#import <Commom/SEGProfileManager.h>
#else
#import "SEGProfileManager.h"
#endif
#if __has_include(<Commom/SEGMenuListModel.h>)
#import <Commom/SEGMenuListModel.h>
#else
#import "SEGMenuListModel.h"
#endif

// 缓存版本（如果此版本修改的菜单结构不适用老版本的缓存数据，可+1）
static NSString const *kMenuCacheVersion = @"1";

@implementation SEGMenuListCacheManager

#pragma mark - 菜单缓存数据
+ (void)menuCache_saveMenuListData:(NSArray<SEGMenuListModel *> *)data action:(NSString *)action {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"menu%@%@_%@.data", action, kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGMenuListModel *> *)menuCache_getMenuListDataWithAction:(NSString *)action {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"menu%@%@_%@.data", action, kAccountUserID, kMenuCacheVersion]];
    NSArray<SEGMenuListModel *> *menuListDataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if ([action isEqualToString:@"Tab"]) { // 底部菜单逻辑
        NSComparator cmptr = ^(SEGMenuListModel *obj1, SEGMenuListModel *obj2) {
            if ([obj1.rn integerValue] > [obj2.rn integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;
            }
            if ([obj1.rn integerValue] < [obj2.rn integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;
            }
            return (NSComparisonResult)NSOrderedSame;
        };
        return [menuListDataArr sortedArrayUsingComparator:cmptr];
    }
    return menuListDataArr;
}

#pragma mark - 页面菜单缓存数据
+ (void)sany_menuCache_saveMenuListData:(NSArray<NSArray<SEGMenuListModel *> *> *)data action:(NSString *)action {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"sany_pageMenu%@%@_%@.data", action, kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<NSArray<SEGMenuListModel *> *> *)sany_menuCache_getMenuListDataWithAction:(NSString *)action {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"sany_pageMenu%@%@_%@.data", action, kAccountUserID, kMenuCacheVersion]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

#pragma mark - 邻里头条缓存数据
+ (void)menuCache_saveNeighborTopLineWithData:(NSArray<SEGNeighborRollViewModel *> *)data {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"NeighborTopLineArchiver%@_%@.data", kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGNeighborRollViewModel *> *)menuCache_getNeighborTopLineData {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"NeighborTopLineArchiver%@_%@.data", kAccountUserID, kMenuCacheVersion]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

#pragma mark - 侧滑推荐位缓存数据
+ (void)menuCache_saveHotNewsWithData:(NSArray<SEGSquareViewModel *> *)data {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"HotNewsArchiver%@_%@.data", kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGSquareViewModel *> *)menuCache_getHotNewsData {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"HotNewsArchiver%@_%@.data", kAccountUserID, kMenuCacheVersion]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

#pragma mark - PGC缓存数据
+ (void)menuCache_savePGCWithData:(NSArray<SEGPgcListModel *> *)data {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@PGC_%@.data", kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGPgcListModel *> *)menuCache_getPGCData {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@PGC_%@.data", kAccountUserID, kMenuCacheVersion]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

#pragma mark - 广告缓存数据
+ (void)menuCache_saveAdsData:(NSArray<SEGAdvertismentModel *> *)data adsType:(SEGAdvlistType)adsType {
    if (!data || data.count <= 0) return;
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"Advertisement%@%@Archiver_%@.data",@(adsType).stringValue, kAccountUserID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGAdvertismentModel *> *)menuCache_getAdsDataWithAdsType:(SEGAdvlistType)adsType {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"Advertisement%@%@Archiver_%@.data",@(adsType).stringValue, kAccountUserID, kMenuCacheVersion]];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

#pragma mark - 常用菜单缓存数据
+ (void)menuCache_saveAllCommonMenuListWithData:(NSArray<SEGMenuListModel *> *)data {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@_%@_AllCommonMenu_%@.data", kAccountUserID, kAccountCommunityID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (NSArray<SEGMenuListModel *> *)menuCache_getAllCommonMenuListData {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@_%@_AllCommonMenu_%@.data", kAccountUserID, kAccountCommunityID, kMenuCacheVersion]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) return [NSArray array];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
+ (void)menuCache_saveCommonMenuWithData:(SEGMenuListModel *)data {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@_%@_CommonMenu_%@.data", kAccountUserID, kAccountCommunityID, kMenuCacheVersion]];
    [NSKeyedArchiver archiveRootObject:data toFile:filePath];
}
+ (SEGMenuListModel *)menuCache_getCommonMenuData {
    NSString *filePath = [self menuCache_getCacheDataWithFilePath:[NSString stringWithFormat:@"SEGArchiver%@_%@_CommonMenu_%@.data", kAccountUserID, kAccountCommunityID, kMenuCacheVersion]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) return [SEGMenuListModel new];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

+ (SEGMenuListModel *)menuCache_getCommonMenuDataWithMaxCount:(NSUInteger) maxCount more:(BOOL)more {
    SEGMenuListModel *model = [self menuCache_getCommonMenuData];
    NSMutableArray *arr = [model.childList mutableCopy];
    if (more) {
        //找出 更多应用
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"action CONTAINS %@",@"action.COMMON_MENULIST"];
        NSArray *filteredArray = [arr filteredArrayUsingPredicate:predicate];
        if (maxCount > 0&filteredArray.count > 0) {
            SEGMenuListModel *moreModel = filteredArray.firstObject;
            [arr removeObject:moreModel];
            NSMutableArray *newArr = [[arr subarrayWithRange:NSMakeRange(0, arr.count > maxCount-1?maxCount-1:arr.count)] mutableCopy];
            [newArr addObject:moreModel];
            model.childList = newArr;
        }
    }else {
        NSArray *newArr = [arr subarrayWithRange:NSMakeRange(0, arr.count > maxCount?maxCount:arr.count)];
        model.childList = newArr;
    }
    return model;
}
 
#pragma mark - Path
/**
 获取缓存数据路径

 @param filePath 具体路径
 @return 存储路径
 */
+ (NSString *)menuCache_getCacheDataWithFilePath:(NSString *)filePath {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return [documentPath stringByAppendingPathComponent:filePath];
}

@end
